home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DICREATE.C < prev    next >
C/C++ Source or Header  |  1995-07-29  |  5KB  |  159 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    dicreate.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to create a data file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //    Globals
  49. //----------------------------------------------------------------------------
  50. #define LF            "\r\n"
  51. #define CTRLZ        "\x1A"
  52.  
  53.  
  54. static CHAR szHeaderFormat[] =            // File header format text
  55.     "Data File v%d.%02d" LF
  56.     "%s" LF
  57.     "Created %2d:%02d:%02d %2d/%02d/%4d." LF
  58.     "%s" LF
  59.     CTRLZ;
  60.  
  61.  
  62. //----------------------------------------------------------------------------
  63. //   Description:    Create a data file.
  64. //                          The file is closed again after it is successfully created.
  65. //    Parameters:    pcsz                File name.
  66. //                                            An extension of '.dat' is appended if no other 
  67. //                                            extension is specified.
  68. //                        pcszUser            Internal name of physical file
  69. //                        pcszId            Application id
  70. //                        usMaxDir            Maximum directory entries
  71. //       Returns:    TRUE if successful.
  72. //----------------------------------------------------------------------------
  73. BOOL FN_E DioCreate(PCSZ pcsz, PCSZ pcszUser, PCSZ pcszId, USHORT usMaxDir)
  74. {
  75. static time_t last_time = 0;
  76.     DATAHDR hdr;
  77.     DATADIR dir;
  78.     CHAR szName[MAX_PATH];
  79.     HF hf;
  80.     USHORT usDir;
  81.     USHORT fs = FL_TRUNCATE|FL_CREATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  82.     time_t now = time(NULL);
  83.     struct tm *tm = localtime(&now);
  84.  
  85.  
  86.     if (pcszId == NULL)
  87.         pcszId = "DATA";
  88.     //
  89.     //    Try to make sure files are created with unique time stamps....
  90.     //
  91.     if (now <= last_time)
  92.         now = last_time + 1;
  93.     //
  94.     //    Validate the size of the various data structures.
  95.     //
  96.     Assert(sizeof(DATAHDR) == DFH_SIZE);
  97.     Assert(sizeof(DATADIR) == DFD_SIZE);
  98.     Assert(strlen(pcszUser) <= sizeof(hdr.chName));
  99.     Assert(((usMaxDir * sizeof(DATADIR)) % sizeof(DATAHDR)) == 0);
  100.     Assert(pcsz && pcszUser);
  101.     Assert(strlen(pcszId) == sizeof(hdr.szAppId));
  102.  
  103.     usMaxDir = usMaxDir ? usMaxDir: 16;
  104.     Assert((LONG)usMaxDir * (LONG)sizeof(DATADIR) <= (LONG)32 _K);
  105.  
  106.     //
  107.     //    Initialize header information
  108.     //
  109.     memset(&hdr, 0, sizeof(hdr));
  110.     sprintf(hdr.szCopyright, szHeaderFormat,
  111.         (int)((DFH_VERION >> 8) & 0x00FF),
  112.         (int)(DFH_VERION & 0x00FF),
  113.         CfgGet(CFG_COPYRIGHT, NULL),
  114.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  115.         tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
  116.         pcszUser);
  117.         
  118.     hdr.lId = DFH_ID;
  119.     hdr.usVersion = DFH_VERION;
  120.     hdr.usDirectoryUsed = 0;
  121.     hdr.usDirectoryEntries = usMaxDir;
  122.     hdr.timetCreated = now;
  123.     hdr.timetModified = now;
  124.     memcpy(hdr.szAppId, pcszId, sizeof(hdr.szAppId));
  125.     memcpy(hdr.chName, pcszUser, strlen(pcszUser));
  126.     hdr.crc = (ULONG)CrcCalc((PBYTE)&hdr, sizeof(hdr) - sizeof(ULONG));
  127.  
  128.     //
  129.     //    Create file, write header and write empty directory entries.
  130.     //
  131.     strcpy(szName, pcsz);
  132.     FnameAppendExt(szName, di.szDataExt, TRUE);
  133.     if (!FnameQualify(szName, di.szDataExt, di.pcszDataPath, 0))
  134.         return FALSE;
  135.         
  136.     if (!FileOpen(&hf, szName, fs, NULL))
  137.         return FALSE;
  138.  
  139.     if (!FileWrite(hf, (PBYTE)&hdr, sizeof(hdr), 0L))
  140.         {
  141.         FileClose(hf);
  142.         return FALSE;
  143.         }
  144.     memset(&dir, 0, sizeof(dir));
  145.     dir.crc = (ULONG)CrcCalc((PBYTE)&dir, sizeof(dir) - sizeof(ULONG));
  146.     for (usDir = 0; usDir < usMaxDir; ++usDir)
  147.         {
  148.        if (!FileWrite(hf, (PBYTE)&dir, sizeof(dir), -1L))
  149.            {
  150.            FileClose(hf);
  151.            return FALSE;
  152.            }
  153.         }
  154.     return FileClose(hf);
  155. }
  156. //----------------------------------------------------------------------------
  157. //------------------------------- End of File --------------------------------
  158. //----------------------------------------------------------------------------
  159.